package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsUserInfoMapper;
import cc.rengu.igas.bsps.common.dao.MchntAcctDetailMapper;
import cc.rengu.igas.bsps.common.dao.ReconDetailQueryMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsUserInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.MchntAcctDetailMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.ReconDetailQueryMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.entity.OutMchntAcctDetail;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntReconDetailBean;
import cc.rengu.igas.bsps.facade.request.ReconDetailQueryRequest;
import cc.rengu.igas.bsps.facade.response.ReconDetailQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 对账单明细查询
 * Created by yuhongwen
 */
public class ReconDetailQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ReconDetailQueryRequest reconDetailQueryRequest = new ReconDetailQueryRequest();
        ConvertUtil.convertOutput(reconDetailQueryRequest);
        return reconDetailQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ReconDetailQueryRequest reconDetailQueryRequest = (ReconDetailQueryRequest) request;
        ReconDetailQueryResponse reconDetailQueryResponse = new ReconDetailQueryResponse();

        ReconDetailQueryMapper reconDetailQueryMapper = new ReconDetailQueryMapperImpl();
        BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();

        String instId = reconDetailQueryRequest.getHeader().getInstId();
        String mchntNo = reconDetailQueryRequest.getMchntNo();
        String startDate = reconDetailQueryRequest.getStartDate();
        String endDate = reconDetailQueryRequest.getEndDate();
        String userId = reconDetailQueryRequest.getUserId();
        rglog.info("必填性校验");
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(startDate) || StringUtil.isEmptyOrNull(endDate)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        rglog.info("时间跨度校验，截止时间范围：" + DateUtil.getBaseDateOffsetMonth(startDate, 12));
        if (DateUtil.compareDate(startDate, endDate) > 0 || DateUtil.compareDate(DateUtil.getBaseDateOffsetMonth(startDate, 12), endDate) < 0) {
            rglog.error("时间区域校验失败！");
            throw new BizException(RespCodeEnum.DATESPAN_CHECK_ERROR.getRespCode(), RespCodeEnum.DATESPAN_CHECK_ERROR.getRespDesc());
        }
        /*设置分页*/
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(reconDetailQueryRequest.getPageIndex());
        pageInfo.setPageSize(reconDetailQueryRequest.getPageSize());
        /**查询入账单明细*/
        MchntAcctDetailMapper mchntAcctDetailMapper = new MchntAcctDetailMapperImpl();
        PageResult mchntAcctDetailPageResult = mchntAcctDetailMapper.selectMchntAcctDetailInfoBySettleDateAndMchntNo(instId, mchntNo, startDate, endDate, pageInfo);
        if (null == mchntAcctDetailPageResult) {
            rglog.error("查询数据为空！");
            throw new BizException(RespCodeEnum.QUERY_NULL.getRespCode(), RespCodeEnum.QUERY_NULL.getRespDesc());
        } else {
            List<MchntReconDetailBean> totalList = new ArrayList<>();
            for (Object object : mchntAcctDetailPageResult.getResult()) {
                OutMchntAcctDetail mchntReconDetail = (OutMchntAcctDetail) object;
                MchntReconDetailBean mchntReconDetailBean = new MchntReconDetailBean();
                mchntReconDetailBean.setTransAmt(mchntReconDetail.getTransAmt());
                mchntReconDetailBean.setMchntNetAmt(mchntReconDetail.getMchntNetAmt());
                mchntReconDetailBean.setMchntTotalFee(mchntReconDetail.getMchntTotalFee());
                mchntReconDetailBean.setMchntActualFee(mchntReconDetail.getMchntActualFee());
                mchntReconDetailBean.setMchntdiscountFee(mchntReconDetail.getMchntDiscountFee());
                mchntReconDetailBean.setMchntOrderId(mchntReconDetail.getOrderId());
                mchntReconDetailBean.setSysSeqNum(mchntReconDetail.getTransSeqNum());
                /*根据商户订单查询付款订单*/
                List<PayOrderInfo> payOrderInfolist = reconDetailQueryMapper.selectPayOrder(instId, mchntReconDetail.getOrderId(), mchntReconDetail.getTransSeqNum());
                if (payOrderInfolist != null) {
                    PayOrderInfo payOrderInfo = payOrderInfolist.get(0);
                    mchntReconDetailBean.setPayerAcctInfo(payOrderInfo.getPayerAcctInfo());
                    mchntReconDetailBean.setTxnName(payOrderInfo.getRemark1());
                    mchntReconDetailBean.setTxnNum(payOrderInfo.getTxnNum());
                    mchntReconDetailBean.setTxnDate(payOrderInfo.getTxnDate());
                    mchntReconDetailBean.setTxnTime(payOrderInfo.getTxnTime());
                    mchntReconDetailBean.setPayType(payOrderInfo.getPayType());

                    /* 查询用户信息*/
                    BspsUserInfoMapper userInfoMapper = new BspsUserInfoMapperImpl();
                    UserInfo userInfo = userInfoMapper.selectBspsUserInfoByUserId(instId, payOrderInfo.getUserId());
                    if (userInfo != null) {
                        mchntReconDetailBean.setCashierId(payOrderInfo.getUserId());
                        mchntReconDetailBean.setUserName(userInfo.getNickName());

                    }
                }
                totalList.add(mchntReconDetailBean);
            }
            reconDetailQueryResponse.setTotalRows(pageInfo.getTotalRows());//总记录数
            reconDetailQueryResponse.setTotalPage(pageInfo.getTotalPage());//总页数
            reconDetailQueryResponse.setMchntReconDetailList(totalList);

        }

        /* 查询结果赋值 */
        MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(instId, mchntNo);
        reconDetailQueryResponse.setMchntName(mchntBaseInfo.getMchntName());
        reconDetailQueryResponse.setMchntNo(mchntBaseInfo.getMchntNo());

        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(reconDetailQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ReconDetailQueryResponse reconDetailQueryResponse = (ReconDetailQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(reconDetailQueryResponse);
    }
}

